package com.hfi.security.core.social.config;

import com.hfi.security.core.social.SocialAuthenticationFilterPostProcessor;
import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * @author ChangLiang
 * @date 2019/8/21
 */
public class HfiSpringSocialConfigurer extends SpringSocialConfigurer {

    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;

    public SocialAuthenticationFilterPostProcessor getSocialAuthenticationFilterPostProcessor() {
        return socialAuthenticationFilterPostProcessor;
    }

    public void setSocialAuthenticationFilterPostProcessor(SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor) {
        this.socialAuthenticationFilterPostProcessor = socialAuthenticationFilterPostProcessor;
    }

    private String filterProcessesUrl;

    public HfiSpringSocialConfigurer(String filterProcessesUrl) {
        this.filterProcessesUrl = filterProcessesUrl;
    }

    /**
     * 改变redirect_uri 由/auth/qq改为第三方应用在qq注册时配置的uri
     * @param object 实际上就是要放到过滤器链中的SocialAuthenticationFilter
     * @param <T>
     * @return
     */
    @Override
    protected <T> T postProcess(T object) {
        SocialAuthenticationFilter filter = (SocialAuthenticationFilter) object;
        filter.setFilterProcessesUrl(filterProcessesUrl);
        // 加入后置处理器 在app环境下将其成功处理器设置为生成令牌的成功处理器
        // 而不是默认的成功处理器的redirect方式
        if (socialAuthenticationFilterPostProcessor!=null) {
            socialAuthenticationFilterPostProcessor.process(filter);
        }
        return (T) filter;
    }
}
